package com.ctg.behavior.calc.impala.transfer.userpath;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.ctg.behavior.calc.common.CtSQLConsts;
import com.ctg.behavior.calc.common.CtSQLParam;
import com.ctg.behavior.calc.impala.report.userpath.CtUserPathUserIdsReport;
import com.ctg.behavior.calc.impala.transfer.CtFilterTransfer;
import com.ctg.behavior.calc.utils.CtCalcSQLUtils;
import com.ctg.behavior.common.calc.CtCalc;
import com.ctg.behavior.common.calc.CtCalcFilter;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class CtUserPathUserIdsTransfer extends CtFilterTransfer {

	@Override
	public String doTransfer(CtCalc calcObject, CtSQLParam sqlParam) {
		if (!(calcObject instanceof CtUserPathUserIdsReport)) {
			log.warn("calcObject is not CtUserPathUserIdsReport, class={}", calcObject.getClass());
			log.warn("calcObject: {}", JSON.toJSON(calcObject));
			return "";
		}
		CtUserPathUserIdsReport report = (CtUserPathUserIdsReport) calcObject;
		log.info("report={}", JSON.toJSONString(report));

		String theEventId = report.getTheEventId();
		String theUserIdsSQL = report.getTheUserIdsSQL();
		String theByProperty = report.getTheByProperty();
		String theColumnDate = report.getTheColumnDate();
		String thePrefix = "uidt" + report.getTheColumnIndex();
		String theJoinPrefix = "uidtj" + report.getTheColumnIndex();

		StringBuilder impalaSQL = new StringBuilder();
		impalaSQL.append(" SELECT ").append(thePrefix).append(".user_id AS userId ");
		impalaSQL.append(" , ").append(thePrefix).append(".event AS event ");
		if (null != theByProperty) {
			impalaSQL.append(" , ").append(thePrefix).append(".").append(theByProperty);
			impalaSQL.append(" AS ").append(theByProperty);
		}

		if (report.getInitEvent()) {
			//源事件类型-开始事件
			impalaSQL.append(" , max(").append(thePrefix).append(".event_time) AS eventTime ");
		} else {
			//源事件类型-结束事件
			impalaSQL.append(" , min(").append(thePrefix).append(".event_time) AS eventTime ");
		}

		// 表结构
		impalaSQL.append(" FROM a_event_parquet_").append(report.getAppId()).append(" ").append(thePrefix).append(" ");

		// 如果不是顶级
		if (!StringUtils.isEmpty(theUserIdsSQL)) {
			// 上级 user_id 的范围
			impalaSQL.append(" JOIN ( ").append(theUserIdsSQL).append(" ) ").append(theJoinPrefix).append(" ");
			impalaSQL.append(" ON ").append(thePrefix).append(".user_id = ").append(theJoinPrefix).append(".userId ");
			//
			impalaSQL.append(" WHERE 1=1 ");

			if (report.getInitEvent()) {
				//源事件类型-开始事件：后继事件发生的时间必须晚于路私径中前一事件
				impalaSQL.append(" AND ").append(thePrefix).append(".event_time > ").append(theJoinPrefix).append(".eventTime ");
			} else {
				//源事件类型-结束事件: 后继事件发生的时间必须早于路私径中前一事件
				impalaSQL.append(" AND ").append(thePrefix).append(".event_time < ").append(theJoinPrefix).append(".eventTime ");
			}

		} else {
			// 若全局筛选中有：用户筛选条件
			if (!report.getFilter().getConditions().isEmpty()) {
				// 如果有用户属性筛选条件，则进行用户表关联( a_user_kudu_{appId})
				impalaSQL.append(" JOIN  a_user_kudu_").append(report.getAppId()).append(" ").append(theJoinPrefix);
				impalaSQL.append(" ON ").append(theJoinPrefix).append(".user_id = ").append(thePrefix).append(".user_id ");
			}
			impalaSQL.append(" WHERE 1=1 ");
			// (起动事件/结束事件)事件条件过滤
			{
				CtCalcFilter filter = report.getSourceEvent().getFilter();
				String conditionsSQL = super.doTransfer(filter, CtSQLParam.createAliasParam(thePrefix));
				if (!CtCalcSQLUtils.isEmptyCondtions(conditionsSQL)) {
					impalaSQL.append(" AND ").append(conditionsSQL);
				}
			}
			// 全局筛选条件过滤：用户属性条件 或 事件属性条件
			if (!report.getFilter().getConditions().isEmpty()) {
				CtSQLParam conditionSQLParam = new CtSQLParam();
				conditionSQLParam.addParam(CtSQLConsts.KEY_USER_ALIAS, theJoinPrefix);
				conditionSQLParam.addParam(CtSQLConsts.KEY_EVENT_ALIAS, thePrefix);
				CtCalcFilter filter = report.getFilter();
				String conditionsSQL = super.doTransfer(filter, conditionSQLParam);
				if (!CtCalcSQLUtils.isEmptyCondtions(conditionsSQL)) {
					impalaSQL.append(" AND ").append(conditionsSQL);
				}
			}
		}

		impalaSQL.append(" AND ").append(thePrefix).append(".event = '").append(theEventId).append("' ");
		impalaSQL.append(" AND ").append(thePrefix).append(".event_date = '").append(theColumnDate).append("' ");

		// 分组
		impalaSQL.append(" GROUP BY ").append(thePrefix).append(".user_id ");
		impalaSQL.append(" , ").append(thePrefix).append(".event ");
		if (null != theByProperty) {
			impalaSQL.append(" , ").append(thePrefix).append(".").append(theByProperty);
		}

		return impalaSQL.toString();
	}

}
